<?php

namespace App\Library\Service;

use App\Library\Logic\AddressLogic;
use App\Library\Logic\AreaLogic;
use App\Library\Requests\Address\AddressAddRequest;
use App\Library\Requests\Address\AddressEditRequest;
use App\Library\Requests\Base\BasePageRequest;
use App\Library\Requests\Base\IdRequest;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;

class AddressService extends BaseService
{
    public function __construct(AddressLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     *
     * @param AddressAddRequest $request
     * @return JsonResponse
     *
     */
    public function add(AddressAddRequest $request): JsonResponse
    {
        $data = self::com($request);
        if ($data instanceof JsonResponse) return $data;
        else return success($data);
    }

    /**
     *
     * @param AddressEditRequest $request
     * @return JsonResponse
     *
     */
    public function edit(AddressEditRequest $request): JsonResponse
    {
        $model = AddressLogic::detail($request->id);
        if (is_null($model) || $model->user_id !== USERID) return fail(433);
        $request->isDefault = $model->is_default;
        $model->delete();

        $data = self::com($request);
        if ($data instanceof JsonResponse) return $data;
        return success($data);
    }

    private function com($request): Model|JsonResponse
    {
        $province = AreaLogic::findByWhere('id', $request->provinceId, ['id', 'name']);
        if (is_null($province)) return fail(433);
        $city = AreaLogic::findAreaInfo($province->id, $request->cityId);
        if (is_null($city)) return fail(433);
        $district = AreaLogic::findAreaInfo($city->id, $request->districtId);
        if (is_null($district)) return fail(433);
        $info = AddressLogic::query()->where(['user_id' => USERID, 'is_default' => 1])
            ->where('is_return', '!=', 1)->first();

        if (is_null($request->isDefault) || !$request->isDefault) $isDefault = false;
        else {
            if (!is_null($info)) {//未设置默认地址
                $info->is_default = false;
                $info->save();
            }
            $isDefault = true;
        }

        return AddressLogic::insertForModel(array_merge($request->all(), [
            'isDefault' => $isDefault,
            'userId' => USERID,
            'province' => $province->name,
            'city' => $city->name,
            'district' => $district->name,
            'fullAddress' => $province->name . $city->name . $district->name . $request->address
        ]));
    }

    /**
     *
     * delete
     * @param IdRequest $request
     * @return JsonResponse
     *
     */
    public function delete(IdRequest $request): JsonResponse
    {
        $model = AddressLogic::detail($request->id);
        if (is_null($model) || $model->user_id !== USERID) return fail(433);
        else $model->delete();
        return success();
    }

    /**
     *
     * @param BasePageRequest $request
     * @return JsonResponse
     *
     */
    public function paging(BasePageRequest $request): JsonResponse
    {
        return success($this->logic->paging($request));
    }

    /**
     * default
     * @param IdRequest $request
     * @return JsonResponse
     *
     */
    public function default(IdRequest $request): JsonResponse
    {
        $model = AddressLogic::detail($request->id);
        if (is_null($model) || $model->user_id !== USERID || $model->is_return === 1) return fail(433);
        AddressLogic::updateByWhere(['isDefault' => 0], ['userId' => USERID, 'isDefault' => 1]);
        $model->is_default = true;
        $model->save();
        return success();
    }

    public function defaultInfo(): JsonResponse
    {
        return success($this->logic->findByWhere([
            'userId' => USERID,
            'isDefault' => true,
            'isReturn' => false
        ]));
    }
}
